#include "Word.h"

Word::Word(int t, int b, int l, int r, int s, string te, int lNr, int wNr, string d)
{
   top = t;
   left = l;
   bottom = b;
   right = r;
   shear = s;
   text = te;
   lineNumber = lNr;
   wordNumber = wNr;
   documentName = d;
   score = -1;
}

Word::Word()
{
   top = 0;
   left = 0;
   bottom = 0;
   right = 0;
   shear = 0;
   text = "";
   lineNumber = 0;
   wordNumber = 0;
   score = -1;
}

Word::~Word()
{
}

int Word::width()
{
    return right - left;
}
    
int Word::height()
{
  return bottom - top + 1;
}

double Word::xcorr()
{
  double angle = M_PI * shear / 180.0; //# shear angle in radians
  return height() * tan(angle);
}

void Word::set_lower_left(int l)
{
  left = (int)(l + xcorr());
}

double Word::get_lower_left()
{
  return left - xcorr();
}

double Word::get_lower_right()
{
  return right - xcorr();
}
        
void Word::set_lower_right(int r)
{
  right = (int)(r - xcorr());
}

void Word::set_real_pos(int l, int w)
{
  set_lower_left(l);
  right = (int)(left + w);
}
        
void Word::resize_up(int amount)
{
  double dx = amount * tan(M_PI * shear / 180.0);
  top -= amount;
  left += dx;
  right += dx;
}  

//void Word::set_height(int y)
//{
//  move_up(top - y);
//}

bool Word::overlaps(Word* other){
  return (other->left < left && left < other->right) 
    || (other->left < right && right < other->right)
    || (left < other->left && other->left < right) 
    || (left < other->right && other->right < right);
}

bool Word::overlapsWithMargin(Word* other, int margin){
  return (other->left < left - margin && left < other->right - margin) 
    || (other->left < right - margin && right < other->right - margin)
    || (left < other->left - margin && other->left < right - margin) 
    || (left < other->right - margin && other->right < right - margin);
}

Word Word::clone()
{
  Word word(top, bottom, left, right, shear, text, lineNumber, wordNumber, documentName);
  return word;
}